* This is the master code to run all codes in order to generate the final results.

***ONE TIME RUN to install necessary commands:
*ssc install rsource
*ssc install reclink2
*ssc install sencode
*ssc install texsave
*ssc install egenmore
*ssc install unique
*ssc install ftools, replace
*ssc install reghdfe, replace
*ssc install ivreg2, replace
*ssc install ranktest, replace
*ssc install ivreghdfe, replace
*ssc install estout, replace
		
clear all
set more off

* 0. Set relative paths
global base = "~/Mokhtar-Scott/India_Project_ReStat_Publication/" //Enter the base path to create all folders
global inputs = "$base/replication/data/raw/"
global working = "$base/replication/data/working/"
global code = "$base/replication/code/"
global clean "$base/replication/data/clean/"
global tempfold = "$base/replication/data/working/temp/"
global BS_fold = "$base/replication/data/working/BS/"

global tables = "$base/replication/tables" //Change this to $base/replication/tables
global figures = "$base/replication/figures" //Change this to $base/replication/figures

global results_fold = "$working/results/"

// Create folders if they don't exist
foreach folder in "$inputs" "$working" "$code" "$clean" "$tempfold" "$BS_fold" "$BS_fold/record" "$tables" "$figures" "$results_fold" {
    capture noisily mkdir "`folder'"
    if _rc == 0 {
        display as text "`folder' created."
    }
    else {
        display as text "`folder' already exists or could not be created."
    }
}
display "All folders have been set up."

// Loop to create folders for local1 to local10
forvalues i = 1/10 {
    foreach subfolder in "$BS_fold/local`i'/" "$BS_fold/local`i'/inputs/" "$BS_fold/local`i'/final/" {
        capture noisily mkdir "`subfolder'"
        if _rc == 0 {
            display as text "`subfolder' created."
        }
        else {
            display as text "`subfolder' already exists or could not be created."
        }
    }
}

foreach subfolder in "Rkl" "Qkl" "Qklm" "Qklme" "CS" {
     capture noisily mkdir "$BS_fold/record/`subfolder'"
     if _rc == 0 {
         display as text "$BS_fold/record/`subfolder' created."
     }
     else {
          display as text "`subfolder' already exists or could not be created."
     }
}

foreach subfolder in "lvl" "time" {
     capture noisily mkdir "$BS_fold/record/CS/kl/`subfolder'"
     if _rc == 0 {
         display as text "$BS_fold/record/CS/kl/`subfolder' created."
     }
     else {
          display as text "`subfolder' already exists or could not be created."
     }
     capture noisily mkdir "$BS_fold/record/CS/klme/`subfolder'"
     if _rc == 0 {
         display as text "$BS_fold/record/CS/klme/`subfolder' created."
     }
     else {
          display as text "`subfolder' already exists or could not be created."
     }
}
display "All subfolders in record folder have been set up."

capture noisily mkdir "$BS_fold/record/Rkl/3lags"

global Rterm_path `"/usr/local/bin/R"' // Ensure that R is installed on your system and verify its installation path. 
global Rterm_options `"--vanilla"'
*

* 1. Data cleaning:
do "$code/cleaning/01_fuzzy_matching_droplist.do"  
do "$code/cleaning/02_clean_products.do"  
do "$code/cleaning/03_fuzzy_match_energy_output.do"  
do "$code/cleaning/04_clean_energy.do" 
do "$code/cleaning/04b_clean_energy_firm_level.do" 
do "$code/cleaning/04c_clean_rawmat.do"  
do "$code/cleaning/05_merging_firm_data.do"  
rsource using "$code/cleaning/06_clean_prowess_wits.R"  
rsource using "$code/cleaning/subfiles/create_figb1.R"  // This is not a data cleaning step. It's just to create Figure B1.
do "$code/cleaning/07_IO_work.do" 
do "$code/cleaning/08_PerpetualInventory.do" 
do "$code/cleaning/09_cleaning.do" 

***Main Text
do "$code/estimation/01_local_full_R_kl.do"

***Robustness
do "$code/estimation/02_local_full_Q_klme.do"
do "$code/estimation/03_local_CS_kl_lvl.do"
do "$code/estimation/04_local_CS_kl_time.do"
do "$code/estimation/05_local_R_kl_3lags.do"
do "$code/estimation/06_local_Q_klme_2lags.do"
do "$code/estimation/07_local_Q_kl.do"
do "$code/estimation/08_local_CS_klme_lvl.do"
do "$code/estimation/09_local_CS_klme_time.do"
do "$code/estimation/10_local_Q_klm.do"


***Prod Tests + tables
do "$code/estimation/AA_ProdTests.do"
do "$code/estimation/BB_ProdTables.do"
do "$code/estimation/CC_DemandTables.do"
do "$code/estimation/DD_add_drop_robustness.do"

****FIGURES AND TABLES
do "$code/estimation/ZZ_Results.do"
